package cn.mhome.merchant.entity.common.result;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

/**
 * 分页类
 *
 * @ClassName: Pager
 * @author: 王军舰 2013-4-9 上午8:46:54
 * @version V1.0
 * 
 */
public class Pager<T> implements Serializable {

	private static final long serialVersionUID = 8337463880134937842L;
	/**
	 * 当前页数据
	 */
	private List<T> data = new ArrayList<T>();
	/**
	 * 当前页码
	 */
	private int currentPage;
	/**
	 * 第一条记录(this.currentPage - 1) * this.onePageSize
	 */
	private int firstResult;
	/**
	 * 总记录数
	 */
	private int totalResults;
	/**
	 * 每页条数
	 */
	private int onePageSize;
	/**
	 * 总页数
	 */
	private int totalPage;
	/**
	 * 下一页页码
	 */
	private int nextPage;
	/**
	 * 上一页页码
	 */
	private int previousPage;

	/**
	 * 单一对象
	 */
	private Object retObj;
	public Object getRetObj() {
		return retObj;
	}

	public void setRetObj(Object retObj) {
		this.retObj = retObj;
	}

	public Pager() {
	}

	public Pager(int currentPage, int onePageSize) {
		if (currentPage > 1)
			this.currentPage = currentPage;
		else
			this.currentPage = 1;

		this.onePageSize = onePageSize;
		this.firstResult = (this.currentPage - 1) * this.onePageSize;
	}

	public int getCurrentPage() {
		return currentPage;
	}

	public void setCurrentPage(int currentPage) {
		this.currentPage = currentPage;
		if (currentPage <= 0)
			this.currentPage = 1;
		if (currentPage > this.totalPage)
			this.currentPage = totalPage;
		this.firstResult = (this.currentPage - 1) * this.onePageSize;

	}

	public int getOnePageSize() {
		return onePageSize;
	}

	public void setOnePageSize(int onePageSize) {
		this.onePageSize = onePageSize;
	}

	public int getTotalResults() {
		return totalResults;
	}

	/**
	 * 设置总记录数，会自动计算出分页数据
	 *
	 * @Title: setTotalResults
	 * @param totalResults
	 * @Author: 王军舰 2013-4-8 上午10:03:53
	 */
	public void setTotalResults(int totalResults) {
		this.totalResults = totalResults;
		if (totalResults % this.onePageSize == 0) {
			this.totalPage = totalResults / this.onePageSize;
		} else {
			this.totalPage = totalResults / this.onePageSize + 1;
		}

		if (this.totalPage == 0) {
			this.totalPage = 1;
		}
		if (this.currentPage > totalPage) {
			this.currentPage = totalPage;
			this.firstResult = (this.currentPage - 1) * this.onePageSize;

		}

		/**
		 * 上一页
		 */
		if (this.currentPage > 1) {
			this.previousPage = this.currentPage - 1;
		} else {
			this.previousPage = 1;
		}

		/**
		 * 下一页
		 */
		if (this.currentPage < this.totalPage) {
			this.nextPage = this.currentPage + 1;
		} else {
			this.nextPage = this.totalPage;
		}
	}

	public int getFirstResult() {
		return firstResult;
	}

	public int getNextPage() {
		if (this.currentPage < this.totalPage) {
			this.nextPage = this.currentPage + 1;
		} else {
			this.nextPage = this.totalPage;
		}
		return nextPage;
	}

	public int getPreviousPage() {
		return previousPage;
	}

	public int getTotalPage() {
		if (totalResults < 0) {
			return -1;
		}
		// long count = totalResults / onePageSize;
		// if (totalResults % onePageSize > 0) {
		// count++;
		// }
		return totalPage;
	}

	public List<T> getData() {
		return data;
	}

	public void setData(List<T> data) {
		this.data = data;
	}

	private String getQueryUrl(String query, String pageNo, boolean flag) {
		String valueString = "";
		if (flag) {
			valueString = String.format(query, pageNo);
		} else {
			valueString = query + pageNo;
		}

		return valueString;

	}

	public void setTotalPage(int totalPage) {
		this.totalPage = totalPage;
	}

	public void setFirstResult(int firstResult) {
		this.firstResult = firstResult;
	}

	public void setNextPage(int nextPage) {
		this.nextPage = nextPage;
	}

	public void setPreviousPage(int previousPage) {
		this.previousPage = previousPage;
	}
	
}